Shader "Prota/Shader-FXAA"
{
    Properties
    {
        _MainTex ("Texture", 2D) = "white" {}
        _EdgeThreshold ("Edge Threshold", Range(0.0, 1.0)) = 0.125
    }
    SubShader
    {
        // No culling or depth
        Cull Off ZWrite Off ZTest Always

        Pass
        {
            CGPROGRAM
            #pragma vertex vert
            #pragma fragment frag

            #include "UnityCG.cginc"

            struct appdata
            {
                float4 vertex : POSITION;
                float2 uv : TEXCOORD0;
            };

            struct v2f
            {
                float2 uv : TEXCOORD0;
                float4 vertex : SV_POSITION;
            };

            v2f vert (appdata v)
            {
                v2f o;
                o.vertex = UnityObjectToClipPos(v.vertex);
                o.uv = v.uv;
                return o;
            }

            // Code below Generated by GPT, and modified by me(Karatoga).
            sampler2D _MainTex;
            float2 _MainTex_TexelSize;
            float _EdgeThreshold;
            
            half4 frag(v2f i) : SV_Target
            {
                // Fetch the texel size
                float2 texelSize = _MainTex_TexelSize;

                // Sample the current pixel and its neighbors
                float4 colorCenter = tex2D(_MainTex, i.uv);
                float3 luma = float3(0.299, 0.587, 0.114);
                float lumaCenter = dot(colorCenter.rgb, luma);
                
                float lumaDown = dot(tex2D(_MainTex, i.uv + float2(0.0, texelSize.y)).rgb, luma);
                float lumaUp = dot(tex2D(_MainTex, i.uv + float2(0.0, -texelSize.y)).rgb, luma);
                float lumaLeft = dot(tex2D(_MainTex, i.uv + float2(-texelSize.x, 0.0)).rgb, luma);
                float lumaRight = dot(tex2D(_MainTex, i.uv + float2(texelSize.x, 0.0)).rgb, luma);

                float lumaMin = min(lumaCenter, min(min(lumaDown, lumaUp), min(lumaLeft, lumaRight)));
                float lumaMax = max(lumaCenter, max(max(lumaDown, lumaUp), max(lumaLeft, lumaRight)));

                // Calculate edge detection
                if (lumaMax - lumaMin < max(_EdgeThreshold, lumaMax * _EdgeThreshold))
                {
                    // return float4(1, 0, 0, 1);
                    return colorCenter;
                }

                // Calculate direction
                float2 dir = float2(
                    -(lumaDown + lumaUp) - 2.0 * lumaCenter,
                    (lumaLeft + lumaRight) - 2.0 * lumaCenter
                );

                // Normalize direction
                float2 dirNorm = normalize(dir);
                dirNorm *= min(abs(dir.x), abs(dir.y)) * texelSize;

                // Sample in the direction of the edge
                // float3 result = 0.5 * (
                //     tex2D(_MainTex, i.uv + dirNorm * (1.0 / 3.0 - 0.5)).rgb +
                //     tex2D(_MainTex, i.uv + dirNorm * (2.0 / 3.0 - 0.5)).rgb);
                float3 result = 0.5 * (
                    tex2D(_MainTex, i.uv + dirNorm * -1).rgb +
                    tex2D(_MainTex, i.uv + dirNorm * 1).rgb);

                // return float4(0, 1, 0, 1);
                // if(length(colorCenter.rgb - result) < 0.1) return float4(1, 0, 0, 1);
                // else return float4(0, 1, 0, 1);
                return float4(result, colorCenter.a);
            }
            ENDCG
        }
    }
}
